package com.hancher.plugins.tab;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

/**
 * 作者 : Hancher ytu_shaoweijie@163.com <br/>
 * 时间 : 2023/3/15 0015 21:46 <br/>
 * 描述 : viewpager与tab绑定的工具类
 */
public class ViewPagerTabUtil {
    public static class Build {
        private ViewPager viewpager;
        private CommonTabLayout viewpagertab;
        private ArrayList<CustomTabEntity> mTabEntities;
        private FragmentManager supportFragmentManager;
        private Toolbar toolbar = null;

        public void build() {
            viewpagertab.setOnTabSelectListener(new OnTabSelectListener() {
                @Override
                public void onTabSelect(int position) {
                    if (viewpagertab.getCurrentTab() != viewpager.getCurrentItem()) {
                        viewpager.setCurrentItem(viewpagertab.getCurrentTab());
                        if (toolbar != null) {
                            toolbar.setTitle(mTabEntities.get(viewpagertab.getCurrentTab()).getTabTitle());
                        }
                    }
                }

                @Override
                public void onTabReselect(int position) {
                }
            });
            viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                @Override
                public void onPageSelected(int position) {
                    if (viewpagertab.getCurrentTab() != viewpager.getCurrentItem()) {
                        viewpagertab.setCurrentTab(viewpager.getCurrentItem());
                        if (toolbar != null) {
                            toolbar.setTitle(mTabEntities.get(viewpager.getCurrentItem()).getTabTitle());
                        }
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                }
            });
            // 设置缓存页面，当前页面的相邻N各页面都会被缓存
            viewpager.setOffscreenPageLimit(mTabEntities.size());
            viewpagertab.setTabData(mTabEntities);
            viewpager.setAdapter(new TabViewPagerAdapter(mTabEntities, supportFragmentManager));
            viewpagertab.setCurrentTab(0);
            viewpager.setCurrentItem(0);
            if (toolbar != null) {
                toolbar.setTitle(mTabEntities.get(0).getTabTitle());
            }
        }

        public ViewPager getViewpager() {
            return viewpager;
        }

        public Build setViewpager(ViewPager viewpager) {
            this.viewpager = viewpager;
            return this;
        }

        public CommonTabLayout getViewpagertab() {
            return viewpagertab;
        }

        public Build setViewpagertab(CommonTabLayout viewpagertab) {
            this.viewpagertab = viewpagertab;
            return this;
        }

        public ArrayList<CustomTabEntity> getmTabEntities() {
            return mTabEntities;
        }

        public Build setmTabEntities(ArrayList<CustomTabEntity> mTabEntities) {
            this.mTabEntities = mTabEntities;
            return this;
        }

        public FragmentManager getSupportFragmentManager() {
            return supportFragmentManager;
        }

        public Build setSupportFragmentManager(FragmentManager supportFragmentManager) {
            this.supportFragmentManager = supportFragmentManager;
            return this;
        }

        public Toolbar getToolbar() {
            return toolbar;
        }

        public Build setToolbar(Toolbar toolbar) {
            this.toolbar = toolbar;
            return this;
        }
    }
}
